package cn.com.huzo.webapp.controller.master;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.MaterialCatalogueEntity;
import huzofw.model.entities.viewmodel.TreeNode;
import cn.com.huzo.service.master.MaterialCatalogueManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 材料目录控制器
 *
 * @author 吴旭
 */
@Controller
public class MaterialCatalogueController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "MaterialCatalogueManager")
    private MaterialCatalogueManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到材料目录编辑画面
     * <p>
     * @return
     */
    @RequestMapping("master/material/catalogue/edit.html")
    public ModelAndView redirect() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.material_catalogue.edit.def");
        return view;
    }

    /**
     * 弹出材料目录选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/material/catalogue/choice.html")
    public ModelAndView redirectChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.material_catalogue.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得材料目录树节点数据。
     *
     * @param key 材料目录标识
     * @return 部们数据对象。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material/catalogue/fetchByKey.html",
        method = RequestMethod.GET)
    public @ResponseBody
    MaterialCatalogueEntity fetchByKey(@RequestParam Long key) throws Exception {
        return _manager.fetchByKey(key);
    }

    /**
     * 取得材料目录数节点数据。
     *
     * @param parentKey 所属材料目录标识
     * @return 该材料目录下的子部们数据节点列表。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material/catalogue/tree.html",
        method = RequestMethod.GET)
    @ResponseBody
    public List<TreeNode> getMaterialCatalogueTreeNode(@RequestParam Long parentKey) throws Exception {
        List<TreeNode> _listMaterialCatalogueTree = _manager.fetchTreeNodeByParentKey(parentKey);
        return _listMaterialCatalogueTree;
    }

    /**
     * 保存材料目录数据。
     * <p>
     * @param instance 要保存的材料目录对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "master/material/catalogue/save.html",
        method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody MaterialCatalogueEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getCatalogueId() > 0) { // 编辑
            rowsAffected = (long) _manager.update(instance); // 返回影响行数
        } else {                             // 新增
            rowsAffected = _manager.insert(instance);        // 返回主键
        }

        return rowsAffected;
    }

    /**
     * 更改所属目录。
     *
     * @param nodeParams 当前目录及父目录标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material/catalogue/changeParentCatalogue.html",
        method = RequestMethod.POST)
    public @ResponseBody
    Integer changeParentCatalogue(@RequestBody final Map<String, Object> nodeParams) throws Exception {
        return _manager.changeParentCatalogue(nodeParams);
    }

    /**
     * 删除材料目录数据。
     *
     * @param instance 材料目录标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material/catalogue/delete.html",
        method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody MaterialCatalogueEntity instance) throws Exception {
        return _manager.delete(instance);
    }
    // </editor-fold>
}
